package com.example.encrypt.utils;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * @description: 双向加密工具类
 * @author: wenwen
 * @date: 2020/3/3 15:11
 * @version: 1.0
 */
public class AESUtils {

    /**
     * 实例化密钥
     */
    private static Key key;

    /**
     * 原始密钥
     */
    private static String KEY_STR = "a_wen";

    /**
     * 编码
     */
    private static String CHARSETNAME = "UTF-8";

    /**
     * 密钥算法
     */
    private static String KEY_ALGORITHM = "AES";

    /**
     * 加密-解密算法 / 工作模式 / 填充方式
     */
    private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 初始化key
     */
    static {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(KEY_STR.getBytes());
            kgen.init(128, random);
            key = kgen.generateKey();
            kgen = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @description: 获取加密后的字符
     *
     * @param str
     * @return:
     * @date: 2020/3/4 16:17
     * @auther: wenwen
    **/
    public static String getEncryptString(String str) {
        try {
            byte[] bytes = str.getBytes(CHARSETNAME);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] doFinal = cipher.doFinal(bytes);
            return Base64.getEncoder().encodeToString(doFinal);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param str
     * @return
     * @description: 对AES加密字符串进行解密
     */
    public static String getDecryptString(String str) {
        try {
            byte[] bytes = Base64.getDecoder().decode(str);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);
             byte[] doFinal = cipher.doFinal(bytes);
            return new String(doFinal, CHARSETNAME);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 对字符串做编码
     *
     * @param param
     * @return
     */
    public static String stringUncode(String param) {
        if (param != null && !param.trim().equals("")) {
            try {
//                param = param.replaceAll("%(?![0-9a-fA-F]{2})", "%25");  //测试%转义
                param = URLEncoder.encode(param, "utf-8");//转码
                // param = URLDecoder.decode(param, "UTF-8");//解码
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return param;
    }


    public static void main(String[] args) {
        String password = "123";
        String pass = AESUtils.getEncryptString(password);
        System.out.println("加密：" + pass);
        System.out.println("解密：" + AESUtils.getDecryptString(pass));

        String s = stringUncode(pass);
        System.out.println(s);

    }

}
